package com.shop.appcontroller.login;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class LogOutController extends SimpleFormController {
	
	
	
	protected ModelAndView showForm(
			HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel)
			throws Exception {	
		
		request.getSession().removeAttribute("userName");
		request.getSession().removeAttribute("userNameSrc");
		request.getSession().invalidate();		 
		return new ModelAndView("redirect:"+this.buildBackUrl(request));
	}

	/**
	 * 创建登陆url链接.
	 * 
	 * @param request
	 * @return
	 */
	private String buildBackUrl(final HttpServletRequest request) {
		StringBuffer result = new StringBuffer("http://"
				+ request.getServerName());
		if (80 != request.getServerPort()) {
			result.append(":").append(request.getServerPort());
		}
		result.append(request.getContextPath());
		result.append("/ebpplogin.htm" );
		return result.toString();
	}
	 
}
